import dayjs from "dayjs";
dayjs.locale('zh-cn');
export function changeFormat(value, type = "YYYY年MM月DD日") {
    return dayjs(value).format(type);
}

export const formatBirth = (id) => {
    let birthday = '';
    id = id ? id : ""
    if (id.length === 15) {
        birthday = '19' + id.slice(6, 12);
    } else if (id.length === 18) {
        birthday = id.slice(6, 14);
    }
    birthday = birthday.replace(/(.{4})(.{2})/, '$1-$2-');
    return birthday;
};

export const formatDate = (dateNumber, isShowMs) => {
    const date = new Date(dateNumber);
    const day = date.getDate();
    const month = date.getMonth();
    const year = date.getFullYear();
    if (isShowMs) {
        const hours = date.getHours();
        const minute = date.getMinutes();
        const seconds = date.getSeconds();
        return `${year} 年 ${month + 1} 月 ${day} 日 ${hours}时 ${minute}分 ${seconds}秒`
    }
    return `${year} 年 ${month + 1} 月 ${day} 日`
}


export function formatYear(id, type) {
    let year = "";
    const dateNow = new Date();

    if (type === 'age') {
        year = formatBirth(id); //-
    }
    return dateNow.getFullYear() - year.substring(0, 4);
}